import ignoreCryList from '../config/ignoreCrypto';
const strToJson = require('@/utils/strToJson');
export default {
  Key: '27F17AD41208C5641B2FD0156E87D6AC87C2F7A19020E1BAD6691981DD3BFE0E4E87CADFAB5E231EDB203A26F5E67C36068FE82BF2B2F82997AD93C679763F01',
  ApiIgnore: ignoreCryList, // 自定义不加密的API
  // 是否需要加密
  needEncryt(option) {
    let ignoreMethod = option.method !== 'post';
    let ignoreCotentType = option.headers['Content-Type'] === 'multipart/form-data' || option.responseType === 'blob';
    let ignoreUrl = this.ApiIgnore.indexOf(option.url) > -1;
    return !(ignoreMethod || ignoreCotentType || ignoreUrl) && process.env.VUE_APP_TRANSPORT_ENCRYPT === 'true';
  },
  // 国密加密
  encrytSM(option) {
    if (this.needEncryt(option)) {
      let originData = option.data || {};
      let encryptData = window.packageEncrypt(JSON.stringify(originData), this.Key);
      option.data = encryptData;
    }
  },
  // 国密解密
  decreptSM(resoponse) {
    return this.needEncryt(resoponse.config) && typeof resoponse.data === 'string'
      ? strToJson.strToJson(window.packageDecrypt(resoponse.data, this.Key))
      : resoponse.data;
  },
};
